package com.ruoyi.web;

import com.alibaba.fastjson2.JSONArray;
import com.mysql.cj.xdevapi.JsonArray;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.framework.web.service.MyLoginService;
import com.ruoyi.framework.web.service.MyTokenService;
import com.ruoyi.system.domain.Company;
import com.ruoyi.system.domain.Mylogin;
import com.ruoyi.system.domain.RegisterCompany;
import com.ruoyi.system.domain.UpdateCompany;
import com.ruoyi.system.service.ICompanyService;
import com.ruoyi.system.service.IRegisterCompanyService;
import com.ruoyi.system.service.IUpdateCompanyService;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@RestController

public class CompanyviewController extends BaseController {

    @Autowired
    private IUpdateCompanyService updateCompanyService;
    @Autowired
    private MyTokenService myTokenService;
    @Autowired
    private ICompanyService companyService;
    @CrossOrigin(origins = "http://localhost/static/html/index.html", allowCredentials = "true")
    @RequestMapping("/myabout")
    public void about(HttpServletRequest request, HttpServletResponse response){
        System.out.println("进入myabout");
        try{
           response.setCharacterEncoding("UTF-8");
            response.addHeader("Access-Control-Allow-Method","POST,GET");//允许访问的方式
            response.setHeader("Access-Control-Allow-Credentials", "true");
            //设置允许多个域名允许跨域集合
            String[] allowDomains = {"http://localhost/static/html/index.html"};
            Set allowOrigins = new HashSet(Arrays.asList(allowDomains));
            String origin = request.getHeader("Origin");
            if(allowOrigins.contains(origin)){
                //设置允许跨域的配置:Access-Control-Allow-Origin： 响应头指定了该响应的资源是否被允许与给定的origin共享
                response.setHeader("Access-Control-Allow-Origin", origin);
            }
           //获取cookie
            Cookie[] cookies = request.getCookies();
            if(cookies==null){
                String token="eyJhbGciOiJIUzUxMiJ9.eyJyb2xlaWQiOjIsImxvZ2lucm9sZSI6ImhybG9naW4ifQ.7O9BcFO5sQoo0A2xrSyhDItxPVOtxUJNJhE1-xAdiebM2DWsIodI0W_EH4cFTNSMnYe1dRGE9BGI1eBHT3kE6w";
                Claims claims=myTokenService.myparseToken(token);
                Long userid = claims.get("roleid", Long.class);
                String role = claims.get("loginrole",String.class);
                System.out.println(role+userid);
                System.out.println("是空的另一个"+Jwts.parser().setSigningKey("resumewebsecrect").parseClaimsJws(token).getBody());
                return;
            }
            for (Cookie cookie:cookies){
                System.out.println(cookie.getName()+" "+cookie.getValue());
                //得到之前的Token
                if(cookie.getName().equals("Admin-Token")){
                    System.out.println(cookie.getValue());
                    String token = cookie.getValue();
                    //解析token

                }
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

        }
    @RequestMapping("/companysignuplist")
    public List<Company> hrsignupCompanylist(){
        //System.out.println("进来了");
        //System.out.println(companyService.selectCompanyAll());
        return companyService.selectCompanyAll();
    }
    @Autowired
    private IRegisterCompanyService registerCompanyService;
    @RequestMapping("/companysignup")
    public AjaxResult companysignup( @RequestParam String companyname,@RequestParam String USCC,@RequestParam String BRN,@RequestParam String operationstatus,@RequestParam String Comptype,@RequestParam String busscope,@RequestParam String indsector,@RequestParam String Legalrep,@RequestParam String registercap,@RequestParam String estabdate,@RequestParam String operatperiod,@RequestParam String registerauth,@RequestParam String registeraddr){
        System.out.println("公司注册");
        RegisterCompany registerCompany=new RegisterCompany();
        registerCompany.setCompanyName(companyname);
        registerCompany.setCompanyUscc(USCC);
        registerCompany.setCompanyBrn(BRN);
        registerCompany.setCompanyOpstatus(operationstatus);
        registerCompany.setCompanyComptype(Comptype);
        registerCompany.setCompanyBusscope(busscope);
        registerCompany.setCompanyIndsector(indsector);
        registerCompany.setCompanyLegalrep(Legalrep);
        registerCompany.setCompanyRegcap(registercap);
        registerCompany.setCompanyEstdate(estabdate);
        registerCompany.setCompanyOpperiod(operatperiod);
        registerCompany.setCompanyRegauth(registerauth);
        registerCompany.setCompanyRegaddr(registeraddr);
        return toAjax(registerCompanyService.insertRegisterCompany(registerCompany));

    }

    @RequestMapping("/updatecompanydetail")
    public AjaxResult updatecompanydetail(@RequestParam String updatecomid,@RequestParam String  updatecomhrid,  @RequestParam String companyname,@RequestParam String USCC,@RequestParam String BRN,@RequestParam String operationstatus,@RequestParam String Comptype,@RequestParam String busscope,@RequestParam String indsector,@RequestParam String Legalrep,@RequestParam String registercap,@RequestParam String estabdate,@RequestParam String operatperiod,@RequestParam String registerauth,@RequestParam String registeraddr){
        UpdateCompany registerCompany = new UpdateCompany();
        registerCompany.setCompanyName(companyname);
        registerCompany.setCompanyUscc(USCC);
        registerCompany.setCompanyBrn(BRN);
        registerCompany.setCompanyOpstatus(operationstatus);
        registerCompany.setCompanyComptype(Comptype);
        registerCompany.setCompanyBusscope(busscope);
        registerCompany.setCompanyIndsector(indsector);
        registerCompany.setCompanyLegalrep(Legalrep);
        registerCompany.setCompanyRegcap(registercap);
        registerCompany.setCompanyEstdate(estabdate);
        registerCompany.setCompanyOpperiod(operatperiod);
        registerCompany.setCompanyRegauth(registerauth);
        registerCompany.setCompanyRegaddr(registeraddr);
        registerCompany.setCompanyId(Long.valueOf(updatecomid));
        registerCompany.setHrId(Long.valueOf(updatecomhrid));
        //companyService.deleteCompanyByCompanyId(Long.valueOf(updatecomid));
        return toAjax(updateCompanyService.insertUpdateCompany(registerCompany));


    }

}
